package com.icecloud.cloud.upms.conf.security;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.icecloud.cloud.upms.common.utils.RestfulResponseBody;

/**
 * 权限验证失败
* @author penghaozhong 
* E-mail:penghaozhong@163.com
* @version 2016年8月3日 下午5:11:25
* 
*/
public class RestAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException, ServletException {
		RestfulResponseBody body = new RestfulResponseBody();
		body.setReason("权限验证失败");
		body.setResultFlag(0);
		body.put("data", accessDeniedException.getMessage());
		
		PrintWriter writer;  
        response.setStatus(200);  
        writer = response.getWriter();  
        writer.write(new ObjectMapper().writeValueAsString(body));  
        writer.flush();  
        writer.close();

	}

}
